package com.zuowei.ycy.user.ui.index;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;

import com.and.yzy.frame.util.RetrofitUtils;
import com.and.yzy.frame.view.dialog.MaterialDialog;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.BusinessHome;
import com.zuowei.ycy.user.http.Merchant;
import com.zuowei.ycy.ui.BaseFgt;
import com.zuowei.ycy.user.util.AppJsonUtil;

import butterknife.Bind;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 商家介绍页面
 */
public class BusinessHomeFgt extends BaseFgt {
    @Bind(R.id.tv_name)
    TextView mTvName;
    @Bind(R.id.rb_pinlun)
    RatingBar mRbPinlun;
    @Bind(R.id.tv_time)
    TextView mTvTime;
    @Bind(R.id.tv_pinjun_time)
    TextView mTvPinjunTime;
    @Bind(R.id.tv_take_price)
    TextView mTvTakePrice;
    @Bind(R.id.tv_address)
    TextView mTvAddress;
    @Bind(R.id.tv_phone)
    TextView mTvPhone;

    private String phone = null;

    @Override
    public int getLayoutId() {
        return R.layout.index_business_home_layout;
    }

    @Override
    public void initData() {
        mTvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (phone != null) {

                    new MaterialDialog(getActivity()).setMDMessage("是否立即拨打客服电话?")
                            .setMDConfirmBtnClick(new MaterialDialog.DialogBtnCallBack() {
                                @Override
                                public void dialogBtnOnClick() {
                                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                                    startActivity(intent);
                                }
                            }).show();
                }
            }
        });
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        showLoadingContentDialog();


    }

    @Override
    public boolean setIsInitRequestData() {
        return true;
    }

    @Override
    public void requestData() {

        String merchant_id = getActivity().getIntent().getExtras().getString("merchant_id");
        String m_id = UserManger.getM_id();
        doHttp(RetrofitUtils.createApi(Merchant.class).merchantInfo(merchant_id, m_id), 1);


    }


    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {

        BusinessHome home = AppJsonUtil.getObject(result, BusinessHome.class);

        mTvName.setText(home.getShop_name());
        mTvTime.setText("营业时间:" + home.getStart_time() + "-" + home.getEnd_time());

        mTvPinjunTime.setText(home.getComment_del_time());
        mTvTakePrice.setText(home.getDelivery_fee());
        mTvAddress.setText("餐厅地址:" + home.getShop_address());

        if (home.getService_telephone().equals("")) {
            mTvPhone.setText("商家客服:" + "暂未设置电话");
        } else {
            mTvPhone.setText("商家客服:" + home.getService_telephone());
            phone = home.getService_telephone();
        }


        mRbPinlun.setRating(Float.parseFloat(home.getComment_score()));


        super.onSuccess(result, call, response, what);
    }
}
